const { theme } = require('../config');
const { env } = require('process');

module.exports = function (engine) {
  engine.registerFilter('get_array_values', (item, key) => {
    if (item && item.length) {
      return item.map((item) => item[key]);
    }
    return [];
  });

  engine.registerFilter('image_url', (item) => {
    return item;
  });
  engine.registerFilter('pubic_front_imgcdn_replace', (item) => {
    return item;
  });

  engine.registerFilter('asset_abs_url', (item) => {
    if (env.NODE_ENV === 'devapi') {
      return `http://localhost:3030/theme/${theme.getTheme()}/assets/${item}`;
    }
    return `/theme/${theme.getTheme()}/assets/${item}`;
  });

  engine.registerFilter('asset_abs_dir_url', (item) => {
    if (env.NODE_ENV === 'devapi') {
      return `http://localhost:3030/theme/${theme.getTheme()}/assets/${item}`;
    }
    return `/theme/${theme.getTheme()}/assets/${item}`;
  });

  engine.registerFilter('money', (item) => {
    return '$' + item;
  });

  engine.registerFilter('public_asset_abs_url', (item) => {
    if (env.NODE_ENV === 'devapi') {
      return `http://localhost:3030/theme/default/assets/${item}`;
    }
    return '/theme/default/assets/' + item;
  });

  engine.registerFilter('public_asset_abs_dir_url', (item) => {
    if (env.NODE_ENV === 'devapi') {
      return `http://localhost:3030/theme/default/assets/${item}`;
    }
    return '/theme/default/assets/' + item;
  });

  engine.registerFilter('checkout_asset_abs_url', (item) => {
    return '/theme/system/assets/' + item;
  });

  engine.registerFilter('public_assets_content', (item) => {
    return `
        <script src="/theme/default/assets/vendor.js"></script>
        <script src="/theme/default/assets/common.js"></script>
        <script src="/theme/default/assets/product.js"></script>
        <script src="/theme/default/assets/collect.js"></script>

        `;
  });
  engine.registerFilter('public_assets_dir_content', (item) => {
    return `
        <script src="/theme/default/assets/vendor.js"></script>
        <script src="/theme/default/assets/common.js"></script>
        <script src="/theme/default/assets/product.js"></script>
        <script src="/theme/default/assets/collect.js"></script>

        `;
  });
  engine.registerFilter('in_array', (item, key) => {
    return item.includes(key);
  });
  engine.registerFilter('getArrayValueByKey', (item, key) => {
    return item[key];
  });

  engine.registerFilter('parse_variables', (item, key) => {
    return item;
  });
  engine.registerFilter('public_front_asset_url', (item, key) => {
    if (!item) {
      return '';
    }
    if (item.indexOf('http') > -1) {
      return item;
    }
    return 'https://cn01-cdn.staticsyy.com' + item;
  });
  engine.registerFilter('lang_direction', (item, key) => {
    return 'ltr'; //rtl ltr
  });
  engine.registerFilter('base64Encode', (item, key) => {
    return Buffer.from(encodeURIComponent(JSON.stringify(item)), 'utf8').toString('base64');
  });
  engine.registerFilter('json', (item) => {
    try {
      // 如果是字符串，尝试解析为JSON
      if (typeof item === 'string') {
        return JSON.stringify(JSON.parse(item) );
      }
      // 如果是对象或数组，返回JSON字符串
      if (typeof item === 'object' && item !== null) {
        return JSON.stringify(item);
      }

      // 其他类型直接返回
      return item;
    } catch (e) {
      // 解析失败时返回原始值
      console.warn('JSON filter error:', e.message);
      return item;
    }
  });

  // 专门用于将对象转换为JSON字符串的过滤器
  engine.registerFilter('strtoarray', (item) => {
    try {
      return JSON.parse(item);
    } catch (e) {
      console.warn('to_json filter error:', e.message, item);
      return false;
    }
  });

  // 专门用于解析JSON字符串的过滤器
  engine.registerFilter('from_json', (item) => {
    try {
      if (typeof item === 'string') {
        return JSON.parse(item);
      }
      return item;
    } catch (e) {
      console.warn('from_json filter error:', e.message);
      return null;
    }
  });
};
